﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

using Aulac.Srv.Db.Provider; using Aulac.Srv.Db.Connect;
using System.Collections.Generic;
using Aulac.Rsc.Ems; using Aulac.Rsc.Ems.Tags;
using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using System.Data.Common;

namespace Aulac.Srv.Db.Data.Ems.MdcFiles.Pnts
{

    public  partial class PntInfoPrvd //: DbAulacDataManager
    {
        /* Code Date: 02/17/2011 by Thangnc */

        private string sqlQuery = "";
        private Db.Provider.IDbDataProvider mDbPrd = null;
        private DbCommand mDbCmd = null;
        public PntInfoPrvd(Db.Provider.IDbDataProvider pDbDataProvider)
        {
            mDbPrd = pDbDataProvider;
            mDbCmd = mDbPrd.DbConnectionEngine.CreateCommand();
        }

        public bool SetPntInfo(XmlDataDocument xmlDoc)
        {
            return SetPntInfoDb(xmlDoc);
        }
   
        /// <summary>
        /// Lay danh sach BN dang kham chua benh tai khoa.
        /// </summary>
        /// <param name="xmlParam">PARAM: DeptPrkID</param>
        public XmlDataDocument GetListPntActiveInDept(XmlDataDocument xmlParam)
        {
            try
            {
                string sDeptPrkID = XmlProcess.GetValueParam(xmlParam, tagItems.Dept.DeptPrkID);
                string[] arrH = new string[] { tagMdcFiles.Pnt.MdcFilePrkID, tagMdcFiles.Pnt.PntPrkID, tagMdcFiles.Pnt.HspInNum, tagMdcFiles.Pnt.PntID, tagMdcFiles.Pnt.PntName, 
                      tagMdcFiles.Pnt.PntBirthday, tagMdcFiles.Pnt.PntAge, tagMdcFiles.Pnt.PntUnitAgeName, tagMdcFiles.Pnt.PntSexName};
                string[] arrL = new string[] { tagMdcFiles.MdcFiles.MdcFileNo };
                string[] arrStatus = new string[] { tagItems.Dept.DeptPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID };
                sqlQuery = string.Format("SELECT mh.{0}, ml.{1}, ps.{3}, ps.{4}, prd.{5}, ps.{6}, ps.{7}", Aulac.Global.Common.StringHelper.ConvertArrayToString(", mh.", arrH), tagMdcFiles.MdcFiles.MdcFileNo, tagItems.Dept.DeptPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID, tagMdcFiles.PntRdcObj.RdcObjPrkID, tagMdcFiles.MdcFiles.ModeTrmtID, tagMdcFiles.Common.PntLogPrkID);
                sqlQuery += string.Format(" FROM {0} mh INNER JOIN {1} ml ", TableNames.MdcFile_PntExam, TableNames.MdcFile_PntCure);
                sqlQuery += string.Format(" ON mh.{0} = ml.{1}", tagMdcFiles.Pnt.MdcFilePrkID, tagMdcFiles.Pnt.MdcFilePrkID);
                sqlQuery += string.Format(" INNER JOIN {0} ps ", TableNames.MdcFile_PntLog);
                sqlQuery += string.Format(" ON mh.{0} = ps.{1}", tagMdcFiles.Pnt.MdcFilePrkID, tagMdcFiles.Pnt.MdcFilePrkID);
                sqlQuery += string.Format(" INNER JOIN {0} prd ", TableNames.MdcFile_PntRdcObj);
                sqlQuery += string.Format(" ON ps.{0} = prd.{1}", tagMdcFiles.PntRdcObj.PntRdcObjPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID);

                sqlQuery += string.Format(" WHERE ps.{0} = ? AND ps.{1} = ? ", tagItems.Dept.DeptPrkID, tagMdcFiles.Exam.PntStatusExam);
                sqlQuery += string.Format("GROUP BY  mh.{0}, ml.{1}, ps.{3}, ps.{4}, prd.{5}, ps.{6}, ps.{7}", Aulac.Global.Common.StringHelper.ConvertArrayToString(", mh.", arrH), tagMdcFiles.MdcFiles.MdcFileNo, tagItems.Dept.DeptPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID, tagMdcFiles.PntRdcObj.RdcObjPrkID, tagMdcFiles.MdcFiles.ModeTrmtID, tagMdcFiles.Common.PntLogPrkID);

                DataTable tb = mDbPrd.ExecuteReaderDataToTable(mDbCmd, sqlQuery, sDeptPrkID, Aulac.Rsc.Ems.StatusPatient.Examming);
                XmlDataDocument xmlDoc = mDbPrd.CreateXmlDocument("", "");
                XmlElement eLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
                Aulac.Global.Data.DataTableProcess.GetValuesToChildElmXml(tb, mDbPrd.DbAulacConnection.CultureInfo, ref eLines);
                return xmlDoc;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Lay danh Sach Bn dang KCB theo hinh thuc dieu tri
        /// </summary>
        /// <param name="xmlParam">PARAM: Modetreatment?</param>
        /// <returns></returns>
        public XmlDataDocument GetListPntActiveByModeTreatment(XmlDataDocument xmlParam)
        {
            try
            {
                string sDeptPrkID = XmlProcess.GetValueParam(xmlParam, tagItems.Dept.DeptPrkID);
                string sCollectModeTreatment = XmlProcess.GetValueParam(xmlParam, tagMdcFiles.MdcFiles.ListModeTrmt);

                string[] arrH = new string[] { tagMdcFiles.Pnt.MdcFilePrkID, tagMdcFiles.Pnt.PntPrkID, tagMdcFiles.Pnt.HspInNum, tagMdcFiles.Pnt.PntID, tagMdcFiles.Pnt.PntName, 
                      tagMdcFiles.Pnt.PntBirthday, tagMdcFiles.Pnt.PntAge, tagMdcFiles.Pnt.PntUnitAgeName, tagMdcFiles.Pnt.PntSexName};
                string[] arrL = new string[] { tagMdcFiles.MdcFiles.MdcFileNo };
                string[] arrStatus = new string[] { tagItems.Dept.DeptPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID };
                sqlQuery = string.Format("SELECT mh.{0}, ml.{1}, ps.{2}, ps.{3}, prd.{4}, ps.{5}, ps.{6}", Aulac.Global.Common.StringHelper.ConvertArrayToString(", mh.", arrH), tagMdcFiles.MdcFiles.MdcFileNo, tagItems.Dept.DeptPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID, tagMdcFiles.PntRdcObj.RdcObjPrkID, tagMdcFiles.MdcFiles.ModeTrmtID, tagMdcFiles.Common.PntLogPrkID);
                sqlQuery += string.Format(" FROM {0} mh INNER JOIN {1} ml ", TableNames.MdcFile_PntExam, TableNames.MdcFile_PntCure);
                sqlQuery += string.Format(" ON mh.{0} = ml.{1}", tagMdcFiles.Pnt.MdcFilePrkID, tagMdcFiles.Pnt.MdcFilePrkID);
                sqlQuery += string.Format(" INNER JOIN {0} ps ", TableNames.MdcFile_PntLog);
                sqlQuery += string.Format(" ON mh.{0} = ps.{1}", tagMdcFiles.Pnt.MdcFilePrkID, tagMdcFiles.Pnt.MdcFilePrkID);
                sqlQuery += string.Format(" INNER JOIN {0} prd ", TableNames.MdcFile_PntRdcObj);
                sqlQuery += string.Format(" ON ps.{0} = prd.{1}", tagMdcFiles.PntRdcObj.PntRdcObjPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID);

                sqlQuery += string.Format(" WHERE  (N'{0}' = '' OR ml.{1} IN({2}))", sCollectModeTreatment, tagMdcFiles.MdcFiles.ModeTrmtID, Aulac.Global.Common.ValueHelper.isEmpty(sCollectModeTreatment)? "''": sCollectModeTreatment);
                sqlQuery += string.Format(" AND (? IS NULL OR ps.{0} = ?) ", tagItems.Dept.DeptPrkID);
                sqlQuery += string.Format(" AND (ps.{0} = ?) ", tagMdcFiles.Exam.PntStatusExam);
                sqlQuery += string.Format("GROUP BY  mh.{0}, ml.{1}, ps.{2}, ps.{3}, prd.{4}, ps.{5}, ps.{6}", Aulac.Global.Common.StringHelper.ConvertArrayToString(", mh.", arrH), tagMdcFiles.MdcFiles.MdcFileNo, tagItems.Dept.DeptPrkID, tagMdcFiles.PntRdcObj.PntRdcObjPrkID, tagMdcFiles.PntRdcObj.RdcObjPrkID, tagMdcFiles.MdcFiles.ModeTrmtID, tagMdcFiles.Common.PntLogPrkID);

                DataTable tb = mDbPrd.ExecuteReaderDataToTable(mDbCmd, sqlQuery, sDeptPrkID, sDeptPrkID, Aulac.Rsc.Ems.StatusPatient.Examming);
                XmlDataDocument xmlDoc = mDbPrd.CreateXmlDocument("", "");
                XmlElement eLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
                Aulac.Global.Data.DataTableProcess.GetValuesToChildElmXml(tb, mDbPrd.DbAulacConnection.CultureInfo, ref eLines);
                return xmlDoc;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public XmlDataDocument FindPntToRegExam(XmlDataDocument xmlParam)
        {
            return FindPntToRegExamDb(xmlParam);
        }

        /// <summary>
        /// Xoa thong tin BN
        /// </summary>
        /// <param name="pPntPrkID"></param>
        /// <returns></returns>
        public bool DelPntInfo(string pPntPrkID)
        {
            return DelPntInfoDb(pPntPrkID);
        }
    }


}
